﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfOfdDataExtractionClient
{
    public class ListBindingItem
    {
        public ListBindingItem(int itemCount)
        {
            Str = new string[itemCount];
        }

        public string[] Str { get; set; }

       

        public string GetStr(int index)
        {
            return Str[index];
        }

        //分析字符串 Str[1]
        public static int ParseStrIndex(string str)
        {
            try
            {
                int start = str.IndexOf("[");
                start++;
                int end = str.IndexOf("]");
                string strIndex = str.Substring(start, end - start);
                int result = int.Parse(strIndex);
                return result;
            }
            catch (Exception ex)
            {
              //  AppLog.Exception(ex);
                return -1;
            }
        }

        public bool ContainKey(string key)
        {
            if (Str == null)
                return false;
            foreach (string item in Str)
            {
                if (item.Contains(key))
                    return true;
                if (item.ToLower().Contains(key.ToLower()))
                    return true;
            }
            return false;
        }

        public object Tag;

        public override string ToString()
        {
            if (Str == null)
                return string.Empty;

            StringBuilder sb = new StringBuilder();
            foreach (string item in Str)
            {
                if (sb.Length == 0)
                {
                    sb.Append(item);
                }
                else
                {
                    sb.Append("\t");
                    sb.Append(item);
                }
            }
            return sb.ToString();
        }

        internal object ToCsv()
        {
            if (Str == null)
                return string.Empty;

            StringBuilder sb = new StringBuilder();
            foreach (string item in Str)
            {
                if (sb.Length == 0)
                {
                    sb.AppendFormat($"{item}");
                }
                else
                {
                    sb.AppendFormat($",{item}");
                }
            }
            return sb.ToString();
        }
    }

    public class ListViewHelper
    {
        public ListView CurListView { get; set; }

        public ListViewHelper(ListView lv)
        {
            CurListView = lv;
        }

        public void ClearAll()
        {
            try
            {
                CurListView.Items.Clear();
            }
            catch (Exception ex)
            {
               // AppLog.Exception(ex);
            }
        }

        public void CopyAll()
        {
            StringBuilder txt = new StringBuilder();
            foreach (object item in CurListView.Items)
            {
                txt.AppendFormat("{0}\r\n", item.ToString());
            }
            Clipboard.SetDataObject(txt.ToString());
        }

        public void CopySel()
        {
            StringBuilder txt = new StringBuilder();
            foreach (object item in CurListView.SelectedItems)
            {
                txt.AppendFormat("{0}\r\n", item.ToString());
            }
            Clipboard.SetDataObject(txt.ToString());
        }

        public void ListViewMenuCmd(string cmdTxt)
        {
            switch (cmdTxt)
            {
                case "复制选中":
                    {
                        CopySel();
                        break;
                    }

                case "复制所有":
                    {
                        CopyAll();
                        break;
                    }

                case "清空所有":
                    {
                        ClearAll();
                        break;
                    }
            }
        }


    }


}